package com.gxgeek.topic;

import com.gxgeek.utils.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author gaoxing
 * @create 2018-05-17 下午2:50
 */
public class Send {
    private final static String EXCHANGE_NAME="test_exchange_topic";

    public static void main(String[] args) throws Exception {
        //获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        //从连接中创建通道
        Channel channel = connection.createChannel();

        //声明Exchange，指定交换机的类型为topic
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        //消息内容  美国新闻
        String message = "美国新闻：川普执行新的医疗政策";
        channel.basicPublish(EXCHANGE_NAME, "usa.news",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //消息内容  美国天气
        message = "美国天气：芝加哥今日天气阴转多云";
        channel.basicPublish(EXCHANGE_NAME, "usa.weather",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //消息内容  中国新闻
        message = "中国新闻：苹果发布最新产品iPhoneXXX";
        channel.basicPublish(EXCHANGE_NAME, "china.news",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //消息内容  中国天气
        message = "中国天气：上海今日天气晴转阵雨";
        channel.basicPublish(EXCHANGE_NAME, "china.weather",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //关闭通道和连接
        channel.close();
        connection.close();
    }

}
